package com.gxa.service.impl;

import com.gxa.dto.ApplyAddDto;
import com.gxa.dto.ApplyDto;
import com.gxa.dto.ApplyGoodsDto;
import com.gxa.dto.EmpDto;
import com.gxa.entity.Apply;
import com.gxa.entity.Emp;
import com.gxa.entity.GoodsDetail;
import com.gxa.mapper.ApplyMapper;
import com.gxa.mapper.GoodsDetailMapper;
import com.gxa.mapper.SupplierMapper;
import com.gxa.service.ApplyService;
import com.gxa.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class ApplyServiceImpl implements ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    private TransactionTemplate transactionTemplate;

    @Override
    public List<Apply> queryAll(ApplyDto applyDto) {  //查询全部信息
        List<Apply> applies = this.applyMapper.queryAll(applyDto);

        return applies;
    }

//    @Override
//    public List<Emp> queryDeot(EmpDto empDto) {
//        List<Emp> emps = this.applyMapper.queryEmp(empDto);
//        return emps;
//    }

    @Override
    public void updateCheck(ApplyAddDto applyAddDto) {   //审核接口

        this.applyMapper.updateCheck(applyAddDto);

    }

    @Override
    public void addApply(ApplyAddDto applyAddDto) {   //新增请购单的保存

        this.applyMapper.addapply(applyAddDto);

        this.applyMapper.addGoods(applyAddDto);
    }

    @Override
    public List<Emp> queryEmp(EmpDto empDto) {  //查询人员
        List<Emp> emps = this.applyMapper.queryEmp(empDto);
        return emps;
    }

    @Override
    public void updateAssign(String applyno,Integer eid) {  //指派员工保存

        this.applyMapper.updateAssign(applyno, eid);

    }

    @Override
    public void updateBackout(String applyno) {  //撤销接口

        this.applyMapper.updateBackout(applyno);


    }

    @Override
    public void deleteApply(String[] ids) {

        applyMapper.deleteApply(ids);
        applyMapper.deleteApplyGoods(ids);

    }


//    @Override
//    public void deleteApply(ApplyAddDto applyAddDto) {  //删除
//
//        applyMapper.deleteApply(applyAddDto);
//        applyMapper.deleteApplyGoods(applyAddDto);
//
//    }




    @Override
    public void updateApply(ApplyAddDto applyAddDto) {  //编辑

        int[] goodsId = this.applyMapper.queryApplyGoodsId(applyAddDto); //查询商品id，和现在过来的数据进行比较

        int [] goods = new int[30];  //创建数组，存放list内的商品id

        System.out.println("查询到的id是---"+Arrays.toString(goodsId));

        List<ApplyGoodsDto> applyGoodsDtoListList = applyAddDto.getApplyGoodsDtoListList();

        int count = 0;
        for (int i = 0;i < applyGoodsDtoListList.size();i++ ){
            ApplyGoodsDto applyGoodsDto = applyGoodsDtoListList.get(i);
            Integer goodsDetailId = applyGoodsDto.getGoodsDetailId();
            goods[i] = goodsDetailId;
            count ++;
        }

//        System.out.println("收到的goodsId是----"+Arrays.toString(goods));
//        System.out.println("goodsId的长度是---"+goodsId.length+"goods的长度是"+goods.length);
        this.applyMapper.updateApply(applyAddDto); //修改基础信息

        applyMapper.updateApplyGoods(applyAddDto); //修改请购商品信息

            int[] a = new int[20];
            if (goodsId.length != count){ //判断之前商品条数和现在商品跳数是否相同
                for (int j = 0; j < goodsId.length; j++){
                    int result = 0; //不同
                    for (int k = 0; k < count;k++){
                        if (goodsId[j] == goods[k]){
                            result = 1;
                        }
                    }
                    if (result == 0){
                        a[j] = goodsId[j];

                    }
                }
                System.out.println("要删除的是--------"+Arrays.toString(a));
                //进行删除
              applyMapper.deleteById(a);

            }
        }


}
